﻿Imports Business
Public Class GarantiasCambiadasForm

    Private Sub bCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bCancelar.Click
        Dispose()
    End Sub

    Private Sub GarantiasCambiadas_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        Titulo()
    End Sub

    Private Sub Titulo()
        With gGrilla
            Dim lista = GarantiaBL.ListGarantiasCambiadas
            .Font = New Drawing.Font("Tahoma", Util.GridFont, FontStyle.Regular, GraphicsUnit.Point)
            If Not IsNothing(lista) Then
                .DataSource = lista
            Else
                Util.Mensaje("No Existen Balones Cambiados")
                .DataSource = Nothing
                Exit Sub
            End If
            Dim ButtonCell As DataGridViewButtonCell = New DataGridViewButtonCell
            ButtonCell.UseColumnTextForButtonValue = True
            Dim ColumnaGarantiaCliente As New DataGridViewButtonColumn

            With ColumnaGarantiaCliente

                .HeaderText = "Cambiar Garantía"
                .Name = "ReingresarGarantia"
                .ReadOnly = False
                '.ValueType = Type.GetType("System.Int64")


                .Resizable = False
                .CellTemplate = ButtonCell
                ButtonCell.Value = "Reingresar Garantía"
                ButtonCell.UseColumnTextForButtonValue = True

            End With

            If Not gGrilla.Columns.Contains("ReingresarGarantia") Then
                gGrilla.Columns.Add(ColumnaGarantiaCliente)
            End If
            .Font = New Drawing.Font("Tahoma", 10, FontStyle.Regular, GraphicsUnit.Point)

            .Columns("ClienteId").Visible = False
            .Columns("GarantiaCambiadaId").Visible = False
            .Columns("GarantiaVentaId").Visible = False
            .Columns("VentaId").Visible = False
            .Columns("fecharecambio").Visible = False
            .RowHeadersVisible = False


            .Columns("Numero").HeaderText = "Número"
            .Columns("FechaVenta").HeaderText = "Fecha Venta"
            .Columns("GarantiaCambiada").HeaderText = "Garantía Cambiada"
            .Columns("GarantiaVenta").HeaderText = "Garantía Venta"
            '.Columns("FechaDevolucion").HeaderText = "Fecha Devolución"


            '.Columns("Numero").DisplayIndex = 1
            '.Columns("Calle").DisplayIndex = 2
            '.Columns("Villa").DisplayIndex = 3
            '.Columns("Rut").DisplayIndex = 4
            '.Columns("Nombre").DisplayIndex = 5
            '.Columns("Fono").DisplayIndex = 6
            '.Columns("FechaVenta").DisplayIndex = 7
            '.Columns("HoraVenta").DisplayIndex = 8
            '.Columns("Descripcion").DisplayIndex = 9
            '.Columns("Garantia").DisplayIndex = 10
            '.Columns("HoraVenta").DisplayIndex = 11
            '.Columns("FechaDevolucion").DisplayIndex = 12
            '.Columns("ReingresarGarantia").DisplayIndex = 14


            For Each col As DataGridViewColumn In .Columns
                'columna.HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter
                col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
            Next
        End With

    End Sub

    Private Sub gGrilla_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles gGrilla.Click
        If gGrilla.Rows.IndexOf(gGrilla.CurrentRow) >= 0 Then
            If gGrilla.CurrentCell.OwningColumn.Name = "ReingresarGarantia" Then
                If gGrilla.Item("GarantiaCambiadaId", gGrilla.CurrentRow.Index).Value <> 0 Then

                    If GarantiaBL.CambioGarantia(gGrilla.Item("VentaId", gGrilla.CurrentRow.Index).Value, gGrilla.Item("garantiaVentaId", gGrilla.CurrentRow.Index).Value, gGrilla.Item("GarantiaCambiadaId", gGrilla.CurrentRow.Index).Value) Then
                        Util.Mensaje("Stock Actualizado")
                        Titulo()
                    Else
                        Util.Mensaje("Error al Reingresar la Garantía")
                    End If
                End If
            End If
        End If
    End Sub
End Class